package com.bestv.task;

import com.bestv.db.*;
import com.bestv.handler.UpdateInfoThread;
import com.bestv.util.DbConstant;
import com.bestv.util.GlobalSettings;
import org.apache.log4j.Logger;

import java.sql.*;

/**
 * Created by charle-chen on 15/11/16.
 */
public class UpdateInfoTask {

    private MysqlDb mysqlDb = null;
    private OracleDb oracleDb = null;
    private int limit = 0;
    private Logger logger = Logger.getLogger(getClass());

    public void updateInfo(){
        int maxCount = 0;
        int offset = 0;

        mysqlDb = (MysqlDb) DbFactory.getDbInstance(DbConstant.MYSQL_DB);

        logger.info("update info table begining ...");
        try{
            String sql = "select count(1) from info where update_flag<>2";
            Statement st = mysqlDb.getConn().createStatement();
            ResultSet rs = st.executeQuery(sql);
            if (rs.next())
            {
                maxCount = rs.getInt(1);
                logger.info("the info table's size: " + maxCount);
            }
        }catch (SQLException e){
            logger.error("update info sql error");
            e.printStackTrace();
            return;
        }finally {
            mysqlDb.closeDb();
        }



        int threadNum = GlobalSettings.getInstance().getThreadNum();
        limit = (int) Math.ceil(maxCount / (threadNum * 1.0));
        for (int i = 0; i < threadNum; i++) {
            UpdateInfoThread thread = new UpdateInfoThread();
            thread.setMaxCount(limit);
            thread.setStart(i * limit);
            thread.start();
        }
    }

}

